<?php


/**
 * Ouputs the entries of the selected labels
 */
function processTagSearch() {
    global $CONFIG, $DB_LANG;
    
    // It requires too much DB load...
    if (empty($_SESSION['labels'])) {
        return false;
    }

    // Retrieve entries data
    $sql = "SELECT SQL_CALC_FOUND_ROWS DISTINCT e.url, e.title, e.contenttoshow 
            FROM entries e
            JOIN entrylabels el ON e.id = el.entryid ";

    // Labels selected 
    if (!empty($_SESSION['labels'])) {
        $sql .= getLabelsSubqueries($_SESSION['labels']);
    }

    // Limit
    $init = ($_SESSION['page'] - 1) * $CONFIG->entriesperpage;
    $sql .= " ORDER BY e.date DESC LIMIT ".$init.", ".$CONFIG->entriesperpage;

    $entries = $DB_LANG->selectResponses($sql);
    
    $nrows = $DB_LANG->selectResponse("SELECT FOUND_ROWS() as total");
    $_SESSION['total'] = $nrows->total;
    
    return $entries;
}


/**
 * Gets the tags cloud
 */
function getLabelsCloud($createmorelabeled = false) {
    
    global $CONFIG, $DB_LANG;
    
    // Query without prevoiusly selected values
    if (empty($_SESSION['labels']) && $createmorelabeled == false) {
    	$sql = "SELECT * FROM morelabeleds";
    	
    // If there are selected labels
    } else {
	    $sql = "SELECT el.labelid as label, count(el.labelid) as count
	            FROM entrylabels el ";
	
	    // Subqueries
	    if (!empty($_SESSION['labels'])) {
	        $sql.= getLabelsSubqueries($_SESSION['labels']);
	    }
	    
	    $sql.= " GROUP BY el.labelid 
	            ORDER BY count DESC
	            LIMIT 0, ".$CONFIG->showntags;
    }
    
    $keywords = $DB_LANG->selectResponses($sql);

    if (!$keywords) {
        return false;
    }

    $keywordsarray = array();
    foreach ($keywords as $key => $keyword) {
        
        // $keyword->label already encoded
        if (empty($keywordsarray[$keyword->label])) {
            $keywordsarray[$keyword->label] = 0;
        }
        $keywordsarray[$keyword->label] += $keyword->count;
    }
    
    ksort($keywordsarray);

    if (empty($keywordsarray)) {
        return false;
    }
    
    return $keywordsarray;
}


function getLabelsSubqueries($labels) {
    
	$i = 97;
	foreach ($labels as $label) {
		
		$char = chr($i);
		$innerjoins[] = 'JOIN entrylabels el'.$char.' ON el.entryid = el'.$char.'.entryid';
		$wheres[] = "el".$char.".labelid = '".$label."'";
		$i++;
	}
	
	$sql = ' '.implode(' ', $innerjoins);
	$sql.= " WHERE ".implode(' AND ', $wheres)." ";
    
    return $sql;
}


/**
 * Self explained
 * @param string $title
 * @param string $url
 * @return boolean
 */
function entryExists($title, $url) {

    global $DB_LANG;
    
    $sql = "SELECT id FROM entries WHERE title = ? OR url = ?";
    if ($DB_LANG->selectResponse($sql, array($title, $url))) {
        return true;
    }
    
    return false;
}


////////////////////////////////////////////////////////////////////


/**
 * Self explained
 * @param string $name
 * @param string $url
 * @return boolean
 */
function blogExists($name, $url) {

    global $DB;
    
    // index_merge
    $sql = "SELECT id FROM blogs WHERE name = ? OR url = ?";

    if ($DB->selectResponse($sql, array($name, $url))) {
        return true;
    }
    
    return false;
}


/**
 * A locales get all
 * @return array
 */
function getSystemLocales() {
    
    global $DB;
    
    $langsquery = "SELECT id, name FROM locales ORDER BY id";
    $results = $DB->selectResponses($langsquery);

    return $results;
}


/**
 * Gets the next blogs to parse
 * 
 * @return array Blogs list
 */
function getPendingBlogs() {
    
    global $DB, $CONFIG;

    $sql = "SELECT DISTINCT id, localeid, url, name FROM blogs 
            WHERE timevisited = ? AND localeid IS NULL 
            ORDER BY id LIMIT 0, ".($CONFIG->Npetitions + 1);
    return $DB->selectResponses($sql, array(0));
}
